import pandas as pd
import matplotlib.pyplot as plt
#import seaborn as sns
#from scipy import stats
import numpy as np
from datetime import datetime


data1 = pd.read_csv('./Flood_frequency_new.csv')
data1['Time'] = pd.to_datetime(data1['Time'], infer_datetime_format=True)


drought = pd.read_csv('./Drought_idnex_monthly_max.csv')
data1['Frequency'] = data1['All']
data1['Flash flood'] = data1['Flash flood']

#----------------------------------------------------------------
drought['D0'] =-drought['D0']
drought['D1'] =-drought['D1']
drought['D2'] =-drought['D2']
drought['D3'] =-drought['D3']
drought['D4']=-drought['D4']


fig, ax = plt.subplots(figsize=(25,10))

plt.fill_between(drought['MapDate'], drought['D0'],drought['D1'],color='#FFD700', alpha=0.8,label='D0')
plt.fill_between(drought['MapDate'], drought['D1'],drought['D2'],color='#FF7D40', alpha=0.8,label='D1')
plt.fill_between(drought['MapDate'], drought['D2'],drought['D3'],color='#FF3030', alpha=0.8,label='D2')
plt.fill_between(drought['MapDate'], drought['D3'],drought['D4'],color='#EE0000', alpha=0.8,label='D3')
plt.fill_between(drought['MapDate'], drought['D4'],color='#800000', alpha=0.8,label='D4')

plt.plot(drought['MapDate'], drought['D0'],color='#FFD700', alpha=0.8)
plt.plot(drought['MapDate'], drought['D1'],color='#FF7D40', alpha=0.8)
plt.plot(drought['MapDate'], drought['D2'],color='#FF3030', alpha=0.8)
plt.plot(drought['MapDate'], drought['D3'],color='#EE0000', alpha=0.8)
plt.plot(drought['MapDate'], drought['D4'],color='#800000', alpha=0.8)


plt.vlines(x=[datetime(2016, 5, 1)], ymin=0.0, ymax=-98, color='black',linewidth = 3)
plt.vlines(x=[datetime(2017, 4, 30)], ymin=0.0, ymax=-98, color='black',linewidth = 3)

# plt.vlines(x=[datetime(2012, 12, 31)], ymin=0.0, ymax=-100, color='black', linewidth = 3)
# plt.vlines(x=[datetime(2012, 1, 1)], ymin=0.0, ymax=-100, color='black', linewidth = 3)

plt.grid(axis='y',ls=':',linewidth=1,which='major')
plt.grid(axis='x',ls='--',linewidth=1,which='major')
plt.text(datetime(2016, 3, 21), -109, 'D-2017', dict(size=50))
#plt.text(datetime(2011, 12, 21), -88, 'D-2012', dict(size=50))
#----------------------------------------------------------------

plt.fill_between(data1['Time'], data1['Frequency'],data1['Flash flood'],color='#B9EDF8', alpha=1,label='All flood events')
plt.fill_between(data1['Time'], data1['Flash flood'],color='#39BAE8', alpha=1,label='Flash flood')

plt.plot(data1['Time'], data1['Frequency'],color='#B9EDF8', alpha=1)
plt.plot(data1['Time'], data1['Flash flood'],color='#39BAE8', alpha=1)
plt.legend(loc='upper right', bbox_to_anchor=(1, 0.35),fontsize=20)
#----------------------------------

plt.vlines(x=[datetime(2015, 10, 1)], ymin=0.0, ymax=80, color='black', linewidth = 3)
plt.vlines(x=[datetime(2016, 5, 1)], ymin=0.0, ymax=80, color='black',linewidth = 3)
plt.text(datetime(2015, 7, 1), 78, 'F-2016', dict(size=50))


plt.vlines(x=[datetime(2018, 8, 1)], ymin=0, ymax=80, color='black',linewidth = 3)
plt.vlines(x=[datetime(2019, 3, 31)], ymin=0, ymax=80, color='black', linewidth = 3)
plt.text(datetime(2018, 5, 1), 78, 'F-2019', dict(size=50))

plt.xticks(fontsize=30)
plt.yticks(fontsize=30)
plt.text(datetime(2022, 12, 1), -90,'Percentage[%]', rotation=270,fontsize=30)
# Formatting x labels
plt.tight_layout()
# Use absolute value for y-ticks
ticks =  ax.get_yticks()
ax.set_yticklabels([int(abs(tick)) for tick in ticks])

plt.twinx()
plt.yticks(fontsize=0.1)
#ax.set_yticklabels([int(abs(tick)) for tick in ticks])
plt.text(datetime(2010, 11, 15),0.6,'Flood frequency',fontsize=30,rotation=90)
plt.text(datetime(2022, 7, 25),0.53,'0',fontsize=30,rotation=0)
plt.text(datetime(2022, 7, 25),0.41,'25',fontsize=30,rotation=0)
plt.text(datetime(2022, 7, 25),0.285,'50',fontsize=30,rotation=0)
plt.text(datetime(2022, 7, 25),0.158,'75',fontsize=30,rotation=0)
plt.text(datetime(2022, 7, 25),0.029,'100',fontsize=30,rotation=0)
plt.savefig("Figure1a_20230222.eps", dpi=350)
plt.show()
